﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;
namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class GoodsList : BasePage
    {
        private string strcompanynameps = "";//企业名称
        private string strMosth = "";//月份
        private string strsortname = "";//类别
        string SortField = "ProductName ASC";
        string strAreaId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                
                if (sq.SortSource == 0)         //上个页面传过来的session，然后赋给本页面的dropSort。
                {
                    droSort.SelectedIndex = 1;
                }
                else if (sq.SortSource == 1)
                {
                    droSort.SelectedIndex = 2;
                }
                else if (sq.SortSource == 2)
                {
                    droSort.SelectedIndex = 3;
                }
                else
                {
                    droSort.SelectedIndex = 0;
                }
                BindList();
            }

        }
        /// <summary>
        /// by张雄  逻辑修改
        /// 2011.5.12
        /// </summary>
        void BindList()
        {

            int t = 0;
            string where = "";
            string order = "";


            SuperviseQuery query = ViewState["query"] as SuperviseQuery;
            strMosth = query.Mid;

            if (!string.IsNullOrEmpty(strMosth)) //如果月份为空 参数有误
            {
                System.Data.DataTable dt = new DataTable();

                if (!droSort.SelectedValue.Equals("-1"))
                {
                    where = "and SortSource =" + droSort.SelectedValue;         //查询本页的SortSource条件
                }

                strAreaId = query.AreaID;

                if (strAreaId.Length == 6)
                {
                    if (strAreaId.Substring(2, 4).Equals("0000"))
                    {
                        where += "and AreaId like'%" + strAreaId.Substring(0, 2) + "%'";
                    }
                    else if (strAreaId.Substring(4, 2).Equals("00"))
                    {
                        where += "and AreaId like'%" + strAreaId.Substring(0, 4) + "%'";
                    }
                    else
                    {
                        where += "and AreaId like'%" + strAreaId + "%'";
                    }
                }
                else
                {
                    where += "and AreaId like'%" + strAreaId + "%'";
                }

                where += " and DayTime like'%" + query.Mid + "%'";

                if (txtProductName.Text.Trim() != "") 
                {
                    where += "and ProductName like '%" + txtProductName.Text.Trim() + "%'";
                }
                if (txtMedicinemodel.Text.Trim() != "")
                {
                    where += "and MedicineModel like '%" + txtMedicinemodel.Text.Trim() + "%'";
                }
                if (txtPack.Text.Trim() != "")
                { 
                    where+="and Pack like '%" + txtPack.Text.Trim() + "%'";
                }
                if (txtUnit.Text.Trim() != "")
                {
                    where += "and Unit like '%" + txtUnit.Text.Trim() + "%'";
                }
                if (txtCompanyName_SC.Text.Trim() != "")
                {
                    where += "and CompanyName_SC like '%" + txtCompanyName_SC.Text.Trim() + "%'";
                }

                order += droSortField.SelectedValue;

                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

                if (query.CompanyType == 0)     //生产企业
                {
                    where += "and CompanyName_SC like '%" + query.CompanyName_PS + "%'";

                    dt = ie.ExecutePage_SC(where, order, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
                }
                else
                {
                    where += "and CompanyName like '%" + query.CompanyName_PS + "%'";

                    dt = ie.ExecutePageCom(where, order, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
                }

                HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
                HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
                AspNetPager1.RecordCount = t;
                gvwGoods.DataSource = dt;
                gvwGoods.DataBind();



            }
            else
            {
                Response.Write("<script>history.go(-1);<script>");
            }
        }

        protected void gvwGoods_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Cells[12].Text = " <a href=\"CompanyGoodsDetail.aspx?mid=" + strMosth + "&gid=" + Server.UrlEncode(e.Row.Cells[1].Text.Trim()) + "&pname=" + Server.UrlEncode(strcompanynameps) + "\">详情</a>";
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SortField = this.droSortField.SelectedValue;
            AspNetPager1.CurrentPageIndex = 1;
            //BindList();
        }

        protected void gvwGoods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyGoodsDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.ProcureCatalogID = e.CommandArgument.ToString();
                query.SortSource =Convert.ToInt32(droSort.SelectedValue);
                var model = new HZ.Data.QueryModel();

                model["AreaId"] = query.AreaID;

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.ProcureCatalogID))
                {
                    Response.Redirect(string.Format("CompanyGoodsDetail.aspx?sessionid={0}", sessionid));
                }
            }
        }

        /// <summary>
        /// 合并表头
        /// create by张雄 2011.5.5
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwGoods_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //取得月份
                    //第一行表头
                    TableCellCollection tc = e.Row.Cells;
                    tc.Clear();
                    tc.Add(new TableHeaderCell());
                    tc[0].Attributes.Add("colspan", "13"); //跨Column
                    tc[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tc[0].Text = HZ.Supervise.Web.Function.FormartDate(sq.Mid) + " " + sq.CompanyName_PS + "</th></tr><tr>";

                    //第2行表头
                    tc.Add(new TableHeaderCell());
                    tc[1].Attributes.Add("bgcolor", "Khaki");
                    tc[1].Text = "";

                    tc.Add(new TableHeaderCell());
                    tc[2].Attributes.Add("bgcolor", "Khaki");
                    tc[2].Text = "通用名";

                    tc.Add(new TableHeaderCell());
                    tc[3].Attributes.Add("bgcolor", "Khaki");
                    tc[3].Text = "剂型";

                    tc.Add(new TableHeaderCell());
                    tc[4].Attributes.Add("bgcolor", "Khaki");
                    tc[4].Text = "采购规格";

                    tc.Add(new TableHeaderCell());
                    tc[5].Attributes.Add("bgcolor", "Khaki");
                    tc[5].Text = "采购单位";

                    tc.Add(new TableHeaderCell());
                    tc[6].Attributes.Add("bgcolor", "Khaki");
                    tc[6].Text = "采购类型";

                    tc.Add(new TableHeaderCell());
                    tc[7].Attributes.Add("bgcolor", "Khaki");
                    tc[7].Text = "生产企业";

                    tc.Add(new TableHeaderCell());
                    tc[8].Attributes.Add("bgcolor", "Khaki");
                    tc[8].Text = "采购金额";

                    tc.Add(new TableHeaderCell());
                    tc[9].Attributes.Add("bgcolor", "Khaki");
                    tc[9].Text = "配送金额";

                    tc.Add(new TableHeaderCell());
                    tc[10].Attributes.Add("bgcolor", "Khaki");
                    tc[10].Text = "配送率";

                    tc.Add(new TableHeaderCell());
                    tc[11].Attributes.Add("bgcolor", "Khaki");
                    tc[11].Text = "入库金额";

                    tc.Add(new TableHeaderCell());
                    tc[12].Attributes.Add("bgcolor", "Khaki");
                    tc[12].Text = "入库率";

                    tc.Add(new TableHeaderCell());
                    tc[13].Attributes.Add("bgcolor", "Khaki");
                    tc[13].Text = "操作";
                    break;
            }
        }

        /// <summary>
        /// 分页
        /// by张雄  2011.5.8
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 导出数据
        /// by张雄  2011.5.16
        /// </summary>
        /// <param name="sender">发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
            {
            int t = 0;
            string where = "";
            string order = "";

            SuperviseQuery query = ViewState["query"] as SuperviseQuery;
            strMosth = query.Mid;

            if (!string.IsNullOrEmpty(strMosth)) //如果月份为空 参数有误
            {

                System.Data.DataTable dt = new DataTable();

                if (!droSort.SelectedValue.Equals("-1"))
                {
                    where = "and SortSource =" + droSort.SelectedValue;         //查询本页的SortSource条件
                }

                strAreaId = query.AreaID;

                if (strAreaId.Length == 6)
                {
                    if (strAreaId.Substring(2, 4).Equals("0000"))
                    {
                        where += "and AreaId like'%" + strAreaId.Substring(0, 2) + "%'";
                    }
                    else if (strAreaId.Substring(4, 2).Equals("00"))
                    {
                        where += "and AreaId like'%" + strAreaId.Substring(0, 4) + "%'";
                    }
                    else
                    {
                        where += "and AreaId like'%" + strAreaId + "%'";
                    }
                }
                else
                {
                    where += "and AreaId like'%" + strAreaId + "%'";
                }


                where += " and DayTime like'%" + query.Mid + "%'";

                if (txtProductName.Text.Trim() != "")
                {
                    where += "and ProductName like '%" + txtProductName.Text.Trim() + "%'";
                }
                if (txtMedicinemodel.Text.Trim() != "")
                {
                    where += "and MedicineModel like '%" + txtMedicinemodel.Text.Trim() + "%'";
                }
                if (txtPack.Text.Trim() != "")
                {
                    where += "and Pack like '%" + txtPack.Text.Trim() + "%'";
                }
                if (txtUnit.Text.Trim() != "")
                {
                    where += "and Unit like '%" + txtUnit.Text.Trim() + "%'";
                }
                if (txtCompanyName_SC.Text.Trim() != "")
                {
                    where += "and CompanyName_SC like '%" + txtCompanyName_SC.Text.Trim() + "%'";
                }

                order += droSortField.SelectedValue;

                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

                if (query.CompanyType == 0)     //生产企业
                {
                    where += "and CompanyName_SC like '%" + query.CompanyName_PS + "%'";

                    dt = ie.ExecutePage_SC(where, order, 1, 0, out t);
                }
                else
                {
                    where += "and CompanyName like '%" + query.CompanyName_PS + "%'";

                    dt = ie.ExecutePageCom(where, order, 1, 0, out t);
                }

                HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
                HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");
                AspNetPager1.RecordCount = t;
                gvwGoods.DataSource = dt;
                gvwGoods.DataBind();

                HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwGoods);

            }
        }
    }
}
